import { defineConfig } from 'vite'
import path from 'node:path'
import electron from 'vite-plugin-electron/simple'
import vue from '@vitejs/plugin-vue'
import tailwindcss from '@tailwindcss/vite'
import commonjs from '@rollup/plugin-commonjs'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    commonjs(),
    // commonjs({
    //   dynamicRequireTargets: [
    //     // 指定需要动态处理的模块
    //     'node_modules/tesseract.js/*.js',
    //     'node_modules/dayjs/*.js',
    //   ]
    // }),
    vue(),
    tailwindcss(),
    electron({
      main: {
        // Shortcut of `build.lib.entry`.
        entry: 'electron/main.ts',
        vite: {
          // optimizeDeps: {
          //   include: ['dayjs', 'dayjs/plugin/*']
          // },
          build: {
            rollupOptions: {
              external: [
                'tesseract.js',
                'bufferutil',
                'utf-8-validate',
                // 其他需要排除的模块
                // 'electron-store',
                // 'exceljs',
                'dayjs'
              ]
            }
          },
        },
      },
      preload: {
        // Shortcut of `build.rollupOptions.input`.
        // Preload scripts may contain Web assets, so use the `build.rollupOptions.input` instead `build.lib.entry`.
        input: path.join(__dirname, 'electron/preload.ts'),
      },
      // Ployfill the Electron and Node.js API for Renderer process.
      // If you want use Node.js in Renderer process, the `nodeIntegration` needs to be enabled in the Main process.
      // See 👉 https://github.com/electron-vite/vite-plugin-electron-renderer
      renderer: process.env.NODE_ENV === 'test'
        // https://github.com/electron-vite/vite-plugin-electron-renderer/issues/78#issuecomment-2053600808
        ? undefined
        : {},
    }),
  ],
  build: {
    commonjsOptions: {
      transformMixedEsModules: true,
      include: [/dayjs/]
    },
    rollupOptions: {
      // 确保 dayjs 不被排除
      external: [],
    },
  },
  resolve:{
    alias:{
      "@":path.resolve(__dirname,'./src'),
      // 强制指向ESM版本的dayjs入口文件
      // dayjs: 'dayjs/esm',
      // 'dayjs': path.resolve(__dirname, 'node_modules/dayjs/dayjs.esm.js')
    }
  }
})
